package com.yunhe.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.yunhe.dao.CarDeviceDao;
import com.yunhe.dao.CarOwnerDao;
import com.yunhe.dao.CarPicDao;
import com.yunhe.dao.CarPriceDao;
import com.yunhe.dao.CarsDao;
import com.yunhe.dao.ClientuserDao;
import com.yunhe.entity.CarDevice;
import com.yunhe.entity.CarOwner;
import com.yunhe.entity.CarPic;
import com.yunhe.entity.CarPrice;
import com.yunhe.entity.Cars;
import com.yunhe.entity.Clientuser;
import com.yunhe.service.CarsService;




@Service("CarsService")
public class CarsServiceImpl implements CarsService {

	@Resource
	private   CarsDao carsDao;
	@Resource
	private  CarDeviceDao carDeviceDao ;
	@Resource
	private CarPriceDao carPriceDao;
	@Resource
	private   CarOwnerDao carOwnerDao;
	@Resource
	private  CarPicDao carPicDao;
	@Resource
   private  ClientuserDao clientuserDao;
	
	
	

	public void setCarDeviceDao(CarDeviceDao carDeviceDao) {
		this.carDeviceDao = carDeviceDao;
	}
	
	public void setCarPriceDao(CarPriceDao carPriceDao) {
		this.carPriceDao = carPriceDao;
	}

	@Override
	public List<Map> listCarBymobileCarcodeCarStatic(
			Map<String, Object> conditions) {
		return  carsDao.listCarBymobileCarcodeCarStatic(conditions);
		
	}
	@Override
	public int saveCar(Cars cars) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public int carAudit(Cars cars) {
		// TODO Auto-generated method stub
		return carsDao.updateByPrimaryKeySelective(cars);
	}
	
	@Override
	public Cars selectByPrimaryKey(String carid) {
		// TODO Auto-generated method stub
		return carsDao.selectByPrimaryKey(carid);
	}
	
	
	
	
	

	
	public void setCarsDao(CarsDao carsDao) {
		this.carsDao = carsDao;
	}
	@Override
	public Map<String, Object> selectCarAndUserByCarId(String carid) {
		// TODO Auto-generated method stub
		return carsDao.selectCarAndUserByCarId(carid);
	}
	

	@Override
	public int carShow(Cars cars) {
		carsDao.updateByPrimaryKeySelective(cars);

		return 1;
	}
	
	@Override
	public int carShow(Cars cars, CarPrice carPrice, CarDevice carDevice) {
		
		if(cars.getCarstate()!=6){
			carsDao.updateByPrimaryKeySelective(cars);
			
		}else{
			carPriceDao.insertSelective(carPrice);
			carDeviceDao.insertSelective(carDevice);
			carsDao.updateByPrimaryKeySelective(cars);
			
		}
		

		 return 1;

	}

	@Override
	public int carBooked(Cars cars, CarOwner carOwner, List  attribute,
			Clientuser clientuser) {
		
		
		try {
			//插入图片
			//获取路径
			for (String pidurl : (ArrayList<String>)attribute) {
				//将路径放入图片表
				CarPic carPic=new CarPic();
				//设置主键
				carPic.setPicid(UUID.randomUUID().toString());
				//设置地址
				carPic.setPidurl(pidurl);
				//设置车辆编号
				carPic.setCarid(cars.getCarid());
				//设置添加时间
				carPic.setAddtime(new Date());
				
				carPicDao.insertSelective(carPic);
				
			}
			
			//插入汽车
			
			carsDao.insertSelective(cars);
			//插入车辆关系
			carOwnerDao.insertSelective(carOwner);
			//车主信息
			clientuserDao.insertSelective(clientuser);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return -1;
		}
		
		
		
		return 0;
	}

	
	


}
